package com.codedancer.campus.manager.api.circle;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.codedancer.campus.common.anno.InterfacePermission;
import com.codedancer.campus.common.core.BaseApi;
import com.codedancer.campus.common.response.ApiResponse;
import com.codedancer.campus.manager.api.circle.ro.CircleExamineRo;
import com.codedancer.campus.manager.api.circle.ro.CirclePageSearchRo;
import com.codedancer.campus.manager.service.ICircleService;
import com.codedancer.campus.manager.service.bo.circle.CircleExamineBo;
import com.codedancer.campus.manager.service.bo.circle.CirclePageSearchBo;
import com.codedancer.campus.manager.service.vo.circle.CirclePageVo;
import com.codedancer.campus.manager.service.vo.circle.CircleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/circle")
@CrossOrigin(methods = {RequestMethod.GET, RequestMethod.POST, RequestMethod.DELETE,
    RequestMethod.PUT}, maxAge = 3600)
@Api(value = "圈子Api", description = "圈子Api", tags = "圈子Api")
public class CircleApi extends BaseApi {

    @Autowired
    private ICircleService circleService;

    @GetMapping("/page")
    @ApiOperation("分页查询圈子")
    @InterfacePermission(encodes = {"superAdmin","treeHoleAdmin"})
    public ApiResponse<IPage<CirclePageVo>> page(@ApiParam(value = "圈子分页查询Ro", required = false)
        CirclePageSearchRo circlePageSearchRo) {
        CirclePageSearchBo circlePageSearchBo = BeanUtil
            .copyProperties(circlePageSearchRo, CirclePageSearchBo.class);
        return success(circleService.page(circlePageSearchBo));
    }

    @PutMapping("/enable/{uuid}")
    @ApiOperation("启用圈子")
    @InterfacePermission(encodes = {"superAdmin","treeHoleAdmin"})
    public ApiResponse<Void> enable(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "圈子uuid", required = true) String uuid) {
        circleService.enable(uuid);
        return success();
    }

    @PutMapping("/disable/{uuid}")
    @ApiOperation("禁用圈子")
    @InterfacePermission(encodes = {"superAdmin","treeHoleAdmin"})
    public ApiResponse<Void> disable(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "圈子uuid", required = true) String uuid) {
        circleService.disable(uuid);
        return success();
    }

    @DeleteMapping("/delete/{uuid}")
    @ApiOperation("删除圈子")
    @InterfacePermission(encodes = {"superAdmin","treeHoleAdmin"})
    public ApiResponse<Void> delete(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "圈子uuid", required = true) String uuid) {
        circleService.delete(uuid);
        return success();
    }

    @PostMapping("/export")
    @ApiOperation("导出圈子")
    @InterfacePermission(encodes = {"superAdmin","treeHoleAdmin"})
    public void export(@ApiParam(value = "圈子分页查询Ro", required = false)
        CirclePageSearchRo circlePageSearchRo, HttpServletRequest request,
        HttpServletResponse response) {
        CirclePageSearchBo circlePageSearchBo = BeanUtil
            .copyProperties(circlePageSearchRo, CirclePageSearchBo.class);
        circleService.export(request, response, circlePageSearchBo);
    }

    @PutMapping("/batch-enable")
    @ApiOperation("批量启用")
    @InterfacePermission(encodes = {"superAdmin","treeHoleAdmin"})
    public ApiResponse<Void> batchEnable(@RequestBody List<String> uuids) {
        circleService.batchEnable(uuids);
        return success();
    }

    @PutMapping("/batch-disable")
    @ApiOperation("批量禁用")
    @InterfacePermission(encodes = {"superAdmin","treeHoleAdmin"})
    public ApiResponse<Void> batchDisable(@RequestBody List<String> uuids) {
        circleService.batchDisable(uuids);
        return success();
    }

    @DeleteMapping("/batch-delete")
    @ApiOperation("批量删除")
    @InterfacePermission(encodes = {"superAdmin","treeHoleAdmin"})
    public ApiResponse<Void> batchDelete(@RequestBody List<String> uuids) {
        circleService.batchDelete(uuids);
        return success();
    }

    @PutMapping("/pass/{uuid}")
    @ApiOperation("通过审核")
    @InterfacePermission(encodes = {"superAdmin","treeHoleAdmin"})
    public ApiResponse<Void> pass(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "圈子uuid", required = true) String uuid,
        @RequestBody @Valid @ApiParam(value = "圈子审核Ro", required = true) CircleExamineRo circleExamineRo) {
        CircleExamineBo circleExamineBo = BeanUtil
            .copyProperties(circleExamineRo, CircleExamineBo.class);
        circleExamineBo.setUuid(uuid);
        circleExamineBo.setCurrentUserUuid(currentLoginUuid());
        circleService.pass(circleExamineBo);
        return success();
    }

    @PutMapping("/no-pass/{uuid}")
    @ApiOperation("未通过审核")
    @InterfacePermission(encodes = {"superAdmin","treeHoleAdmin"})
    public ApiResponse<Void> noPass(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "圈子uuid", required = true) String uuid,
        @RequestBody @Valid @ApiParam(value = "圈子审核Ro", required = true) CircleExamineRo circleExamineRo) {
        CircleExamineBo circleExamineBo = BeanUtil
            .copyProperties(circleExamineRo, CircleExamineBo.class);
        circleExamineBo.setUuid(uuid);
        circleExamineBo.setCurrentUserUuid(currentLoginUuid());
        circleService.noPass(circleExamineBo);
        return success();
    }

    @GetMapping("/detail/{uuid}")
    @ApiOperation("获取圈子详情")
    @InterfacePermission(encodes = {"superAdmin","treeHoleAdmin"})
    public ApiResponse<CircleVo> detail(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "圈子uuid", required = true) String uuid) {
        return success(circleService.detail(uuid));
    }
}
